package me.kagami.autotesthelper;


import javax.swing.*;
import java.awt.event.*;

public class AutoTestHelpDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField textField4;
    private JButton readMeButton;
    private JRadioButton yamlRadioButton;
    private JRadioButton jsonRadioButton;
    private ButtonGroup buttonGroup;
    private AutoTestHelpDto autoTestHelpDto = new AutoTestHelpDto();

    public AutoTestHelpDto getAutoTestHelpDto() {
        return autoTestHelpDto;
    }

    public AutoTestHelpDialog() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        readMeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ReadMeDialog dialog = new ReadMeDialog();
                dialog.pack();
                dialog.setVisible(true);
            }
        });
        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        buttonGroup = new ButtonGroup();
        buttonGroup.add(yamlRadioButton);
        buttonGroup.add(jsonRadioButton);
    }

    private void onOK() {
        autoTestHelpDto.setClassName(textField4.getText());
        autoTestHelpDto.setYaml(yamlRadioButton.isSelected());
        autoTestHelpDto.setJson(jsonRadioButton.isSelected());
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    public static void main(String[] args) {
        AutoTestHelpDialog dialog = new AutoTestHelpDialog();
        dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }

}